﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using DevExpress.Xpf.Core;
using UIT.ScrumTool.Client.ControlService;
using UIT.ScrumTool.Client.Utilities.UserGridView;
using Xceed.Wpf.DataGrid;

namespace UIT.ScrumTool.Client.Controller
{
    public class UserCtrl
    {
        #region Thuoc Tinh

        //lay kenh User
        private static readonly UserServiceClient userServiceClient = new UserServiceClient();

        #endregion

        #region Lay danh sach cac user

        //Return table user
        public static void UserTable(UserBindingListComplete dataList)
        {
            try
            {
                dataList.Clear();
                dataList.BeginInit();
                List<UserDto> lUDto = userServiceClient.UserGetByAll().ToList();
                foreach (UserDto userDto in lUDto)
                {
                    dataList.Add(new UserEditTable("US" + userDto.Id.ToString().Trim(), userDto.LoginName,
                                                   userDto.Password, userDto.LastName, userDto.FistName, userDto.Email,
                                                   userDto.IMType, userDto.IM, userDto.CompanyPostion, userDto.Profile));
                }
                dataList.EndInit();
            }
            catch (Exception)
            {
                return;
            }
        }

        #endregion

        #region Delete User

        public static void UserDelete(UserBindingListComplete userBindingListComplete, DataGridControl dataGridControl)
        {
            if (dataGridControl.SelectedItems.Count > 0)
            {
                try
                {
                    var userEditTable = (UserEditTable) dataGridControl.SelectedItem;
                    string x = userEditTable.ID.Replace("US", "");
                    userServiceClient.UserDelete(Convert.ToInt16(x));
                    userBindingListComplete.RemoveAt(dataGridControl.SelectedIndex);
                    dataGridControl.SelectedItems.Clear();
                    dataGridControl.UpdateLayout();
                }
                catch
                {
                    DXMessageBox.Show("Have  a user...", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }

        #endregion

        #region Insert User

        public static void UserInsert(UserEditTable userEditTable)
        {
            var userDto = new UserDto();
            userDto.LoginName = userEditTable.LoginName;
            userDto.Password = userEditTable.Password;
            userDto.LastName = userEditTable.LastName;
            userDto.FistName = userEditTable.FirstName;
            userDto.Email = userEditTable.Email;
            userDto.IMType = userEditTable.IMType;
            userDto.IM = userEditTable.IM;
            userDto.Picture = null;
            userDto.CompanyPostion = userEditTable.CompanyPosition;
            userDto.Profile = userEditTable.Profile;
            userServiceClient.UserInsert(userDto);
        }

        #endregion

        #region Refresh

        public static void UserRefresh(UserBindingListComplete dataList)
        {
            try
            {
                dataList.Clear();
                dataList.BeginInit();
                List<UserDto> lUDto = userServiceClient.UserGetByAll().ToList();
                foreach (UserDto userDto in lUDto)
                {
                    dataList.Add(new UserEditTable("US" + userDto.Id.ToString().Trim(), userDto.LoginName,
                                                   userDto.Password, userDto.LastName, userDto.FistName, userDto.Email,
                                                   userDto.IMType, userDto.IM, userDto.CompanyPostion, userDto.Profile));
                }
                dataList.EndInit();
            }
            catch (Exception)
            {
                return;
            }
        }

        #endregion

        #region Update Users

        public static void UserUpdate(UserEditTable userEditTable)
        {
            var userDto = new UserDto();
            userDto.Id = Convert.ToInt16(userEditTable.ID.Replace("US", ""));
            userDto.LoginName = userEditTable.LoginName;
            userDto.Password = userEditTable.Password;
            userDto.LastName = userEditTable.LastName;
            userDto.FistName = userEditTable.FirstName;
            userDto.Email = userEditTable.Email;
            userDto.IMType = userEditTable.IMType;
            userDto.IM = userEditTable.IM;
            userDto.Picture = null;
            userDto.CompanyPostion = userEditTable.CompanyPosition;
            userDto.Profile = userEditTable.Profile;
            userServiceClient.UserUpdate(userDto);
        }

        #endregion

        // Get List Users In DB, return List UserDto
        public static List<UserDto> UserGetByAll()
        {
            try
            {
                return userServiceClient.UserGetByAll().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static UserDto UserGetById(int id)
        {
            try
            {
                return userServiceClient.UserGetById(id);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}